package com.daffodil.storage.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 分页组件
 * @author yweijian
 * @date 2019年12月12日
 * @version 1.0
 */
@ApiModel("分页参数")
public class Page {

    /** 记录数长度 */
    @ApiModelProperty(hidden = true)
    private transient int length = 0;

    /** 游标 */
    @ApiModelProperty(hidden = true)
    private transient int start = -1;

    /** 每页记录数 */
    @ApiModelProperty(name = "pageSize", value = "每页记录数")
    private int pageSize = 15;

    /** 当前页 */
    @ApiModelProperty(name = "pageNumber", value = "当前页")
    private int pageNumber = 1;

    /** 当前页(别名) */
    @ApiModelProperty(name = "pageNum", value = "当前页(别名)")
    private int pageNum = 1;

    /** 总记录数 */
    @ApiModelProperty(hidden = true)
    private int totalRow = 0;

    /** 总页数 */
    @ApiModelProperty(hidden = true)
    private int totalPage = 1;
    
    public Page() {
        super();
    }

    public Page(int pageSize, int pageNumber) {
        super();
        this.pageSize = pageSize;
        this.pageNum = pageNumber;
        this.pageNumber = pageNumber;
    }

    public int getLength() {
        return this.length;
    }

    public void setLength(int length) {
        this.length = length;
        set(this.start, length);
    }

    public int getStart() {
        return this.start;
    }

    public void setStart(int start) {
        this.start = start;
        set(start, this.length);
    }

    public void set(int start, int length) {
        if ((start == -1) || (length == 0)) {
            return;
        }
        this.pageNumber = (start / length + 1);
        this.pageSize = length;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNum = pageNumber;
        this.pageNumber = pageNumber;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
        this.pageNumber = pageNum;
    }

    public int getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(int totalRow) {
        this.totalRow = totalRow;
    }

    public int getTotalPage() {
        int size = this.totalRow / this.pageSize;
        int mod = this.totalRow % this.pageSize;
        if (mod != 0) {
            size++;
        }
        if (size == 0) {
            size = 1;
        }
        this.totalPage = size;
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * 分页游标起始
     * @return
     */
    public int getFromIndex() {
        return (getPageNumber() - 1) * this.pageSize;
    }

    /**
     * 分页游标起止
     * @return
     */
    public int getToIndex() {
        return getPageNumber() * this.pageSize;
    }

}
